package com.wc._16届国特冲刺营._07数据结构_哈希破题法.团建;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/28 21:25
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072910&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * mp 反向存值, dfs 深度搜索
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010, M = N << 2;
    static int[] a = new int[N], b = new int[N];
    static int[] h1 = new int[N], h2 = new int[N], e = new int[M], ne = new int[M];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        for (int i = 1; i <= m; i++) b[i] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(h1, a, b);
            add(h2, b, a);
        }
        for (int i = 1; i < m; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(h2, a, b);
            add(h2, b, a);
        }

        out.println(dfs(1, -1, 1, -1));
        out.flush();
    }

    static int dfs(int u1, int f1, int u2, int f2) {
        int res = 0;
        if (a[u1] != b[u2]) return res;
        Map<Integer, Integer> mp = new HashMap<>();
        for (int i = h1[u1]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == f1) continue;
            mp.put(a[j], j);
        }

        for (int i = h2[u2]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == f2) continue;
            if (mp.containsKey(b[j])) {
                res = Math.max(res, dfs(mp.get(b[j]), u1, j, u2));
            }
        }

        return res + 1;
    }

    static void add(int[] h, int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

